<?php
/**
 * 固定传播基金双月凭证
 * @author qianxun
 *
 */
class spreadProItemModel extends MY_Model{
    function spreadProItemModel(){
        parent::MY_Model();
        $this->table = 'SpreadProItem';
    }
    /**
     * 更新父级金额
     * {@inheritDoc}
     * @see MY_Model::updateParentMoneys()
     */
    function updateParentMoneys($parentId){
        $this->CI->load->model('spreadProcessModel');
        $this->CI->spreadProcessModel->updateMoney($parentId);
    }
    /**
     * 获取父级ID
     * {@inheritDoc}
     * @see MY_Model::getParentId()
     */
    function getParentId($data){
        return isset($data['procId']) ? (int)$data['procId'] : 0;
    }
    /**
     * 包装数据
     * {@inheritDoc}
     * @see MY_Model::packing()
     */
    function packing($list = array()){
        //时间单位配置
        $this->CI->load->model('timeUnitModel');
        $timeUnitList = $this->CI->timeUnitModel->getCacheData('timeUnitList');
        //角色列表
        $roleList = $this->config->item('roleList');
        //审核结果
        $auditRs = $this->config->item('spreadItemAuditRs');
        foreach ($list as $k => $v){
            //时间单位
            $v['timeUnitStr'] = isset($timeUnitList[$v['year']][$v['timeUnit']]) ? $timeUnitList[$v['year']][$v['timeUnit']] : '';
            //凭证
            $v['voucher'] = $v['voucher'] ? unserialize($v['voucher']) : array();
            //审核结果
            if ($v['auditRs'] > 0){
                $v['auditRsStr'] = isset($roleList[$v['auditRole']]) ? $roleList[$v['auditRole']].'已审核' : '';
                $v['auditRsStr'] .= isset($auditRs[$v['auditRs']]) ? ','.$auditRs[$v['auditRs']] : '';
            }else{
                $v['auditRsStr'] = isset($auditRs[$v['auditRs']]) ? $auditRs[$v['auditRs']] : '';;
            }
            $list[$k] = $v;
        }
        return $list;
    }
    /**
     * 更新金额
     * @param number $itemId
     */
    function updateMoney($itemId){
        $findData = $this->getNewData($itemId);
        if(!$findData){
            return false;
        }
        $data = array();
        //申请金额
        $data['applyMoney'] = $findData['voucher'] ? $findData['monthApplyMoney'] : 0;
        //扣罚金额
        $data['lostMoney'] = 0;
        //未投放,未审核、虚假投放（扣除整个双月费用）
        if (!in_array($findData['auditRs'], array(1))){
            $data['lostMoney'] = $findData['monthApplyMoney'];
        }
        //审核金额
        if ($findData['auditRs'] > 0){
            $data['auditMoney'] = $data['applyMoney'] - $data['lostMoney'];
        }
        //修改
        $this->edit($itemId, $data);
    }
    /**
     * 获取明细项列表，并以双月分组
     * @param unknown $procId
     * @return unknown
     */
    function getItemListByTimeUnit($procId){
        $itemList = $this->getNewList(array('procId'=>$procId), '', '', '', 'timeUnit ASC');
        $projectIds = array();
        foreach ($itemList as $v){
            in_array($v['projectId'], $projectIds) || $projectIds[] = (int)$v['projectId'];
        }
        $CI =& get_instance();
        $CI->load->model('spreadProjectModel');
        $projectList = $CI->spreadProjectModel->getNewList(array('id'=>array('in',$projectIds)));
        foreach ($itemList as $k => $v){
            $v['projectData'] = isset($projectList[$v['projectId']]) ? $projectList[$v['projectId']] : array();
            if ($v['projectData']){
                $itemList[$k] = $v;                
            }else{
                unset($itemList[$k]);
            }
        }
        $newItemList = array();
        foreach ($itemList as $v){
            isset($newItemList[$v['timeUnit']]) || $newItemList[$v['timeUnit']] = array();
            $newItemList[$v['timeUnit']][] = $v;
        }
        return $newItemList;
    }
    /**
     * 获取指定主线流程的金额合计
     * @param number $procId
     */
    function getMoney($procId){
        $data = array(
            'applyMoney' => 0,//申请金额
            'auditMoney' => 0,//审核金额
            'lostMoney' => 0,//扣罚金额
        );
        $findList = $this->getList(array('procId' => $procId));
        foreach ($findList as $v){
            $data['applyMoney'] += $v['applyMoney'];
            $data['auditMoney'] += $v['auditMoney'];
            $data['lostMoney'] += $v['lostMoney'];
        }
        return $data;
    }
    
}